<?php

class ControllerShippingSpeedy extends Controller {

    private $error = array();

    public function index() {
        $this->language->load('shipping/speedy');

        $this->document->setTitle($this->language->get('heading_title'));

        require_once(DIR_SYSTEM . 'library/speedy.php');
        $speedy = new Speedy($this->registry);
        if ($speedy->getError()) {
            $this->error['warning'] = $speedy->getError();
        }

        $this->load->model('setting/setting');

        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
            $this->model_setting_setting->editSetting('speedy', $this->request->post);

            $this->session->data['success'] = $this->language->get('text_success');

            $this->redirect($this->url->link('extension/shipping', 'token=' . $this->session->data['token'], 'SSL'));
        }

        $this->data['heading_title'] = $this->language->get('heading_title');

        $this->data['text_enabled'] = $this->language->get('text_enabled');
        $this->data['text_disabled'] = $this->language->get('text_disabled');
        $this->data['text_all_zones'] = $this->language->get('text_all_zones');
        $this->data['text_yes'] = $this->language->get('text_yes');
        $this->data['text_no'] = $this->language->get('text_no');
        $this->data['text_select_all'] = $this->language->get('text_select_all');
        $this->data['text_unselect_all'] = $this->language->get('text_unselect_all');

        $this->data['entry_server_address'] = $this->language->get('entry_server_address');
        $this->data['entry_username'] = $this->language->get('entry_username');
        $this->data['entry_password'] = $this->language->get('entry_password');
        $this->data['entry_name'] = $this->language->get('entry_name');
        $this->data['entry_telephone'] = $this->language->get('entry_telephone');
        $this->data['entry_workingtime_end'] = $this->language->get('entry_workingtime_end');
        $this->data['entry_allowed_methods'] = $this->language->get('entry_allowed_methods');
        $this->data['entry_pricing'] = $this->language->get('entry_pricing');
        $this->data['entry_fixed_price'] = $this->language->get('entry_fixed_price');
        $this->data['entry_free_shipping_total'] = $this->language->get('entry_free_shipping_total');
        $this->data['entry_free_method_city'] = $this->language->get('entry_free_method_city');
        $this->data['entry_free_method_intercity'] = $this->language->get('entry_free_method_intercity');
        $this->data['entry_back_documents'] = $this->language->get('entry_back_documents');
        $this->data['entry_back_receipt'] = $this->language->get('entry_back_receipt');
        $this->data['entry_default_weight'] = $this->language->get('entry_default_weight');
        $this->data['entry_packing'] = $this->language->get('entry_packing');
        $this->data['entry_label_printer'] = $this->language->get('entry_label_printer');
        $this->data['entry_insurance'] = $this->language->get('entry_insurance');
        $this->data['entry_fragile'] = $this->language->get('entry_fragile');
        $this->data['entry_from_office'] = $this->language->get('entry_from_office');
        $this->data['entry_office'] = $this->language->get('entry_office');
        $this->data['entry_documents'] = $this->language->get('entry_documents');
        $this->data['entry_fixed_time'] = $this->language->get('entry_fixed_time');
        $this->data['entry_taking_date'] = $this->language->get('entry_taking_date');
        $this->data['entry_currency'] = $this->language->get('entry_currency');
        $this->data['entry_weight_class'] = $this->language->get('entry_weight_class');
        $this->data['entry_order_status'] = $this->language->get('entry_order_status');
        $this->data['entry_geo_zone'] = $this->language->get('entry_geo_zone');
        $this->data['entry_status'] = $this->language->get('entry_status');
        $this->data['entry_sort_order'] = $this->language->get('entry_sort_order');

        $this->data['button_save'] = $this->language->get('button_save');
        $this->data['button_cancel'] = $this->language->get('button_cancel');
        $this->data['button_testcredentials'] = $this->language->get('check_credentials_btn_label');

        $this->data['token'] = $this->session->data['token'];

        if (isset($this->error['warning'])) {
            $this->data['error_warning'] = $this->error['warning'];
        } else {
            $this->data['error_warning'] = '';
        }

        if (isset($this->error['username'])) {
            $this->data['error_username'] = $this->error['username'];
        } else {
            $this->data['error_username'] = '';
        }
        
        $this->data['error_username_credentials'] = 
                $this->language->get('error_username_credentials_chk');
        
        $this->data['error_password_credentials'] = 
                $this->language->get('error_password_credentials_chk');

        if (isset($this->error['password'])) {
            $this->data['error_password'] = $this->error['password'];
        } else {
            $this->data['error_password'] = '';
        }

        if (isset($this->error['name'])) {
            $this->data['error_name'] = $this->error['name'];
        } else {
            $this->data['error_name'] = '';
        }

        if (isset($this->error['telephone'])) {
            $this->data['error_telephone'] = $this->error['telephone'];
        } else {
            $this->data['error_telephone'] = '';
        }

        if (isset($this->error['default_weight'])) {
            $this->data['error_default_weight'] = $this->error['default_weight'];
        } else {
            $this->data['error_default_weight'] = '';
        }

        if (isset($this->error['allowed_methods'])) {
            $this->data['error_allowed_methods'] = $this->error['allowed_methods'];
        } else {
            $this->data['error_allowed_methods'] = '';
        }

        $this->data['breadcrumbs'] = array();

        $this->data['breadcrumbs'][] = array(
            'text' => $this->language->get('text_home'),
            'href' => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
            'separator' => false
        );

        $this->data['breadcrumbs'][] = array(
            'text' => $this->language->get('text_shipping'),
            'href' => $this->url->link('extension/shipping', 'token=' . $this->session->data['token'], 'SSL'),
            'separator' => ' :: '
        );

        $this->data['breadcrumbs'][] = array(
            'text' => $this->language->get('heading_title'),
            'href' => $this->url->link('shipping/speedy', 'token=' . $this->session->data['token'], 'SSL'),
            'separator' => ' :: '
        );

        $this->data['action'] = $this->url->link('shipping/speedy', 'token=' . $this->session->data['token'], 'SSL');

        $this->data['cancel'] = $this->url->link('extension/shipping', 'token=' . $this->session->data['token'], 'SSL');

        if (!empty($this->request->post['speedy_server_address'])) {
            $this->data['speedy_server_address'] = $this->request->post['speedy_server_address'];
        } elseif ($this->config->get('speedy_server_address')) {
            $this->data['speedy_server_address'] = $this->config->get('speedy_server_address');
        } else {
            $this->data['speedy_server_address'] = 'https://www.speedy.bg/eps/main01.wsdl';
        }

        if (isset($this->request->post['speedy_username'])) {
            $this->data['speedy_username'] = $this->request->post['speedy_username'];
        } else {
            $this->data['speedy_username'] = $this->config->get('speedy_username');
        }

        if (isset($this->request->post['speedy_password'])) {
            $this->data['speedy_password'] = $this->request->post['speedy_password'];
        } else {
            $this->data['speedy_password'] = $this->config->get('speedy_password');
        }

        if (isset($this->request->post['speedy_name'])) {
            $this->data['speedy_name'] = $this->request->post['speedy_name'];
        } else {
            $this->data['speedy_name'] = $this->config->get('speedy_name');
        }

        if (isset($this->request->post['speedy_telephone'])) {
            $this->data['speedy_telephone'] = $this->request->post['speedy_telephone'];
        } else {
            $this->data['speedy_telephone'] = $this->config->get('speedy_telephone');
        }

        if (isset($this->request->post['speedy_workingtime_end_hour'])) {
            $this->data['speedy_workingtime_end_hour'] = $this->request->post['speedy_workingtime_end_hour'];
        } else {
            $this->data['speedy_workingtime_end_hour'] = $this->config->get('speedy_workingtime_end_hour');
        }

        if (isset($this->request->post['speedy_workingtime_end_min'])) {
            $this->data['speedy_workingtime_end_min'] = $this->request->post['speedy_workingtime_end_min'];
        } else {
            $this->data['speedy_workingtime_end_min'] = $this->config->get('speedy_workingtime_end_min');
        }

        if (isset($this->request->post['speedy_allowed_methods'])) {
            $this->data['speedy_allowed_methods'] = $this->request->post['speedy_allowed_methods'];
        } elseif ($this->config->get('speedy_allowed_methods') && $this->request->server['REQUEST_METHOD'] != 'POST') {
            $this->data['speedy_allowed_methods'] = $this->config->get('speedy_allowed_methods');
        } else {
            $this->data['speedy_allowed_methods'] = array();
        }

        if (isset($this->request->post['speedy_pricing'])) {
            $this->data['speedy_pricing'] = $this->request->post['speedy_pricing'];
        } else {
            $this->data['speedy_pricing'] = $this->config->get('speedy_pricing');
        }

        if (isset($this->request->post['speedy_fixed_price'])) {
            $this->data['speedy_fixed_price'] = $this->request->post['speedy_fixed_price'];
        } else {
            $this->data['speedy_fixed_price'] = $this->config->get('speedy_fixed_price');
        }

        if (isset($this->request->post['speedy_free_shipping_total'])) {
            $this->data['speedy_free_shipping_total'] = $this->request->post['speedy_free_shipping_total'];
        } else {
            $this->data['speedy_free_shipping_total'] = $this->config->get('speedy_free_shipping_total');
        }

        if (isset($this->request->post['speedy_free_method_city'])) {
            $this->data['speedy_free_method_city'] = $this->request->post['speedy_free_method_city'];
        } else {
            $this->data['speedy_free_method_city'] = $this->config->get('speedy_free_method_city');
        }

        if (isset($this->request->post['speedy_free_method_intercity'])) {
            $this->data['speedy_free_method_intercity'] = $this->request->post['speedy_free_method_intercity'];
        } else {
            $this->data['speedy_free_method_intercity'] = $this->config->get('speedy_free_method_intercity');
        }

        if (isset($this->request->post['speedy_back_documents'])) {
            $this->data['speedy_back_documents'] = $this->request->post['speedy_back_documents'];
        } else {
            $this->data['speedy_back_documents'] = $this->config->get('speedy_back_documents');
        }

        if (isset($this->request->post['speedy_back_receipt'])) {
            $this->data['speedy_back_receipt'] = $this->request->post['speedy_back_receipt'];
        } else {
            $this->data['speedy_back_receipt'] = $this->config->get('speedy_back_receipt');
        }

        if (isset($this->request->post['speedy_default_weight'])) {
            $this->data['speedy_default_weight'] = $this->request->post['speedy_default_weight'];
        } else {
            $this->data['speedy_default_weight'] = $this->config->get('speedy_default_weight');
        }

        if (isset($this->request->post['speedy_packing'])) {
            $this->data['speedy_packing'] = $this->request->post['speedy_packing'];
        } else {
            $this->data['speedy_packing'] = $this->config->get('speedy_packing');
        }

        if (isset($this->request->post['speedy_label_printer'])) {
            $this->data['speedy_label_printer'] = $this->request->post['speedy_label_printer'];
        } else {
            $this->data['speedy_label_printer'] = $this->config->get('speedy_label_printer');
        }

        if (isset($this->request->post['speedy_insurance'])) {
            $this->data['speedy_insurance'] = $this->request->post['speedy_insurance'];
        } else {
            $this->data['speedy_insurance'] = $this->config->get('speedy_insurance');
        }

        if (isset($this->request->post['speedy_fragile'])) {
            $this->data['speedy_fragile'] = $this->request->post['speedy_fragile'];
        } else {
            $this->data['speedy_fragile'] = $this->config->get('speedy_fragile');
        }

        if (isset($this->request->post['speedy_from_office'])) {
            $this->data['speedy_from_office'] = $this->request->post['speedy_from_office'];
        } else {
            $this->data['speedy_from_office'] = $this->config->get('speedy_from_office');
        }

        if (isset($this->request->post['speedy_office_id'])) {
            $this->data['speedy_office_id'] = $this->request->post['speedy_office_id'];
        } else {
            $this->data['speedy_office_id'] = $this->config->get('speedy_office_id');
        }

        if (isset($this->request->post['speedy_documents'])) {
            $this->data['speedy_documents'] = $this->request->post['speedy_documents'];
        } else {
            $this->data['speedy_documents'] = $this->config->get('speedy_documents');
        }

        if (isset($this->request->post['speedy_fixed_time'])) {
            $this->data['speedy_fixed_time'] = $this->request->post['speedy_fixed_time'];
        } else {
            $this->data['speedy_fixed_time'] = $this->config->get('speedy_fixed_time');
        }

        if (isset($this->request->post['speedy_taking_date'])) {
            $this->data['speedy_taking_date'] = $this->request->post['speedy_taking_date'];
        } else {
            $this->data['speedy_taking_date'] = $this->config->get('speedy_taking_date');
        }

        if (isset($this->request->post['speedy_currency'])) {
            $this->data['speedy_currency'] = $this->request->post['speedy_currency'];
        } else {
            $this->data['speedy_currency'] = $this->config->get('speedy_currency');
        }

        if (isset($this->request->post['speedy_weight_class_id'])) {
            $this->data['speedy_weight_class_id'] = $this->request->post['speedy_weight_class_id'];
        } else {
            $this->data['speedy_weight_class_id'] = $this->config->get('speedy_weight_class_id');
        }

        if (isset($this->request->post['speedy_order_status_id'])) {
            $this->data['speedy_order_status_id'] = $this->request->post['speedy_order_status_id'];
        } else {
            $this->data['speedy_order_status_id'] = $this->config->get('speedy_order_status_id');
        }

        if (isset($this->request->post['speedy_geo_zone_id'])) {
            $this->data['speedy_geo_zone_id'] = $this->request->post['speedy_geo_zone_id'];
        } else {
            $this->data['speedy_geo_zone_id'] = $this->config->get('speedy_geo_zone_id');
        }

        if (isset($this->request->post['speedy_status'])) {
            $this->data['speedy_status'] = $this->request->post['speedy_status'];
        } else {
            $this->data['speedy_status'] = $this->config->get('speedy_status');
        }

        if (isset($this->request->post['speedy_sort_order'])) {
            $this->data['speedy_sort_order'] = $this->request->post['speedy_sort_order'];
        } else {
            $this->data['speedy_sort_order'] = $this->config->get('speedy_sort_order');
        }

        $this->load->model('localisation/currency');

        $this->data['currencies'] = $this->model_localisation_currency->getCurrencies();

        $this->load->model('localisation/weight_class');

        $this->data['weight_classes'] = $this->model_localisation_weight_class->getWeightClasses();

        $this->load->model('localisation/order_status');

        $this->data['order_statuses'] = $this->model_localisation_order_status->getOrderStatuses();

        $this->load->model('localisation/geo_zone');

        $this->data['geo_zones'] = $this->model_localisation_geo_zone->getGeoZones();

        $this->data['services'] = $speedy->getServices();

        $this->data['offices'] = $speedy->getOffices();

        $this->data['pricings'] = array(
            'calculator' => $this->language->get('text_calculator'),
            'calculator_fixed' => $this->language->get('text_calculator_fixed'),
            'fixed' => $this->language->get('text_fixed_price'),
            'free' => $this->language->get('text_free_shipping')
        );

        $this->template = 'shipping/speedy.tpl';
        $this->children = array(
            'common/header',
            'common/footer'
        );

        $this->response->setOutput($this->render());
    }

    public function checkcredentials() {
        
        $this->language->load('shipping/speedy');

        //$this->document->setTitle($this->language->get('heading_title'));

        require_once(DIR_SYSTEM . 'library/speedy.php');
        $speedy = new Speedy($this->registry);

        if (!$this->request->post['username'] || !$this->request->post['username']) {
            $this->response->setOutput(json_encode(array('error' => 1, 'message' => $this->language->get('incorrect_user_and_pass'))));

            return;
        }

        $username = $this->request->post['username'];
        $password = $this->request->post['password'];

        $isCredentialsCorrect = $speedy->checkCredentials($username, $password);

        if ($isCredentialsCorrect) {
            $this->response->setOutput(json_encode(array('ok' => 1, 'message' => $this->language->get('correct_user_and_pass'))));
        } else {
            $this->response->setOutput(json_encode(array('error' => 1, 'message' => $this->language->get('incorrect_user_and_pass'))));
        }
    }

    protected function validate() {
        if (!$this->user->hasPermission('modify', 'shipping/speedy')) {
            $this->error['warning'] = $this->language->get('error_permission');
        }

        if (!$this->request->post['speedy_server_address']) {
            $this->request->post['speedy_server_address'] = 'https://www.speedy.bg/eps/main01.wsdl';
        }

        if (!$this->request->post['speedy_username']) {
            $this->error['username'] = $this->language->get('error_username');
        }

        if (!$this->request->post['speedy_password']) {
            $this->error['password'] = $this->language->get('error_password');
        }

        if (!$this->request->post['speedy_name']) {
            $this->error['name'] = $this->language->get('error_name');
        }

        if (!$this->request->post['speedy_telephone']) {
            $this->error['telephone'] = $this->language->get('error_telephone');
        }

        if (!$this->request->post['speedy_default_weight']) {
            $this->error['default_weight'] = $this->language->get('error_default_weight');
        }

        if (!isset($this->request->post['speedy_allowed_methods'])) {
            $this->error['allowed_methods'] = $this->language->get('error_allowed_methods');
        }

        if (!$this->error) {
            return true;
        } else {
            return false;
        }
    }

    public function install() {
        $this->load->model('setting/setting');

        $shipping_data = array(
            'shipping_estimator' => 0,
            'shipping_status' => 1,
            'shipping_sort_order' => $this->config->get('shipping_sort_order')
        );

        $this->model_setting_setting->editSetting('shipping', $shipping_data);

        $cod_data = array(
            'cod_status' => 0
        );

        $this->model_setting_setting->editSetting('cod', $cod_data);

        $this->load->model('shipping/speedy');

        $this->model_shipping_speedy->createTables();

        @mail('support@extensadev.com', 'Speedy Shipping Module installed', HTTP_CATALOG . ' - ' . $this->config->get('config_name') . "\r\n" . 'version - ' . VERSION . "\r\n" . 'IP - ' . $this->request->server['REMOTE_ADDR'], 'MIME-Version: 1.0' . "\r\n" . 'Content-type: text/plain; charset=UTF-8' . "\r\n" . 'From: ' . $this->config->get('config_owner') . ' <' . $this->config->get('config_email') . '>' . "\r\n");
    }

    public function uninstall() {
        $this->load->model('shipping/speedy');

        $this->model_shipping_speedy->deleteTables();
    }

}

?>